package com.zack.gank.gankandroid.main.main.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.orhanobut.logger.Logger;
import com.zack.gank.gankandroid.gankandroid.R;
import com.zack.gank.gankandroid.main.find.adapter.SelectFindAdapter;
import com.zack.gank.gankandroid.main.find.bean.Gank;
import com.zack.gank.gankandroid.sexy.SexyDetailActivity;
import com.zack.gank.gankandroid.utils.ScreenUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Android on 2017/7/17.
 */
public class SeaxyAdapter extends RecyclerView.Adapter<SeaxyAdapter.SeaxyHolder> {

    private Context mContext;
    private List<Gank> ganks;
    private List<Integer> listHeight;
    public SeaxyAdapter(Context context,List<Gank> list) {
        this.mContext=context;
        this.ganks=list;
        listHeight=new ArrayList<>();
        for(int i=0;i<ganks.size();i++){
            listHeight.add((int)(600+Math.random()*150));
        }
    }

    @Override
    public SeaxyHolder onCreateViewHolder(ViewGroup parent, int i) {
       View view= LayoutInflater.from(mContext).inflate(R.layout.seaxy_layout_adapter,parent,false);
        return new SeaxyHolder(view);
    }

    @Override
    public void onBindViewHolder(final SeaxyHolder seaxyHolder, final int i) {
//        ViewGroup.LayoutParams params = seaxyHolder.ivSeaxy.getLayoutParams();
//        params.height=listHeight.get(i);
//        seaxyHolder.ivSeaxy.setLayoutParams(params);
//        Glide.with(mContext).load(ganks.get(i).url).into(seaxyHolder.ivSeaxy);
       Glide.with(mContext).load(ganks.get(i).url).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                seaxyHolder.ivSeaxy.setImageBitmap(resource);
                Palette palette= Palette.generate(resource);
                Palette.Swatch swatch =palette.getLightVibrantSwatch();
                if(swatch!=null){
                    seaxyHolder.ll_author.setBackgroundColor(swatch.getRgb());
                    seaxyHolder.tv_author_name.setTextColor(swatch.getTitleTextColor());
                    seaxyHolder.tvAuthor.setTextColor(swatch.getBodyTextColor());
                }
            }
        });

        seaxyHolder.tvAuthor.setText(ganks.get(i).who);

        seaxyHolder.ivSeaxy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext, SexyDetailActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("ganks", (Serializable) ganks);
                intent.putExtra("gank",bundle);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return ganks.size();
    }



    public void addListData(List<Gank> listGank){
        for(int i=0;i<listGank.size();i++){
            this.ganks.add(listGank.get(i));
            listHeight.add((int)(600+Math.random()*150));
        }
    }

    public class SeaxyHolder extends RecyclerView.ViewHolder {
        ImageView ivSeaxy;
        TextView tvAuthor;
        TextView tv_author_name;
        LinearLayout ll_author;
        public SeaxyHolder(View itemView) {
            super(itemView);
            ivSeaxy= (ImageView) itemView.findViewById(R.id.iv_seaxy);
             tvAuthor = (TextView) itemView.findViewById(R.id.tv_author);
            tv_author_name = (TextView) itemView.findViewById(R.id.tv_author_name);
            ll_author = (LinearLayout) itemView.findViewById(R.id.ll_author);
        }
    }
}
